package logic

import (
	"cart_srv/cart"
	"cart_srv/internal/svc"
	"cart_srv/model/mysql"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type CartAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLogic {
	return &CartAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartAddLogic) CartAdd(in *cart.AddCartRequest) (*cart.AddCartResponse, error) {
	// todo: 新增购物车

	c := mysql.CartZye{
		UserId:      int(in.UserId),
		GoodsId:     int(in.GoodsId),
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  float64(in.GoodsPrice),
		Num:         int(in.Num),
		GoodsAvatar: in.GoodsAvatar,
	}

	err := c.CartCreate()
	if err != nil {
		logx.Error("新增失败", err)
		return nil, err
	}

	return &cart.AddCartResponse{CartId: int64(c.ID)}, nil
}
